﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using GalaSoft.MvvmLight.Messaging;
using MSSQL.DBSync.UI.Common.Messages;
using MessageBox = System.Windows.MessageBox;

namespace MSSQL.DBSync.UI.Components.MainWindow
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class MainWindow : Window , IMainWindow
    {
        public MainWindow(IMainWindowVM vm)
        {
            InitializeComponent();

            DataContext = vm;

            ////注册消息中心
            Messenger.Default.Register<Type>(this, MessageTopics.APP_GotoPage, NaviToConten);
            Messenger.Default.Register<string>(this, MessageTopics.APP_ShowMessage, ShowInfo);
            Messenger.Default.Register<string>(this, MessageTopics.APP_ShowError, ShowError);
            Messenger.Default.Register<Action<string>>(this, MessageTopics.APP_OpenDir, OpenDir);
            Messenger.Default.Register<Action<string>>(this, MessageTopics.APP_OpenJSONFile, OpenJsonFile );
            Messenger.Default.Register<Action<string>>(this, MessageTopics.APP_SaveSqlFile, SaveSqlFile);
            Messenger.Default.Register<string>(this, MessageTopics.APP_GoBack, AppGoBack);
            Messenger.Default.Register<Type>(this, MessageTopics.APP_GotoPage, NaviToConten);
            Messenger.Default.Register<(string mgs, Action<int> cb)>(this,MessageTopics.APP_Confirm, ShowConfirm);
            //Messenger.Default.Register<Type>(this, MessageTopics.APP_Title, NaviToConten);

            MainContainer.Navigated += MainContainer_Navigated;
        }

        private void MainContainer_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            var cont = MainContainer.Content;
            var iType = cont.GetType().GetInterfaces()
                .Where(x => x.Assembly == Assembly.GetAssembly(typeof(MainWindow)))
                .FirstOrDefault();

            if (iType != null)
            {
                Messenger.Default.Send(iType, MessageTopics.APP_Title);
            }
        }

        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="contentKey"></param>
        public void NaviToConten(Type contentKey)
        {
            var cntPage = App.GetContainer().GetService(contentKey);
            MainContainer.Content = cntPage;
            //Messenger.Default.Send(contentKey, MessageTopics.APP_Title);
        }

        /// <summary>
        /// 返回上一页
        /// </summary>
        /// <param name="contentKey"></param>
        public void AppGoBack(string title = "")
        {
            MainContainer.GoBack();
            //var cont = MainContainer.Content;
            //var iType = cont.GetType().GetInterfaces()
            //    .Where(x => x.Assembly == Assembly.GetAssembly(typeof(MainWindow))).FirstOrDefault();
            //if (iType != null)
            //{
            //    Messenger.Default.Send(iType, MessageTopics.APP_Title);
            //}
        }

        /// <summary>
        /// 通知消息
        /// </summary>
        /// <param name="msgConent"></param>
        public void ShowInfo(string msgConent)
        {
            var res = MessageBox.Show(msgConent, "通知",MessageBoxButton.OK,MessageBoxImage.Information);
        }

        /// <summary>
        /// 通知错误
        /// </summary>
        /// <param name="msgConent"></param>
        public void ShowError(string msgConent)
        {
            var res = MessageBox.Show(msgConent, "错误",MessageBoxButton.OK,MessageBoxImage.Error);
        }


        /// <summary>
        /// 消息0
        /// </summary>
        /// <param name="para"></param>
        public void ShowConfirm((string mgs, Action<int> cb) para)
        {
            var res = MessageBox.Show(para.mgs, "请确认", MessageBoxButton.YesNo, MessageBoxImage.Warning,MessageBoxResult.No);
            if (res == MessageBoxResult.Yes)
            {
                para.cb(1);
            }

            para.cb(0);
        }

        /// <summary>
        /// 打开文件目录
        /// </summary>
        /// <param name="cb">成功回调</param>
        public void OpenDir(Action<string> cb)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            // Set the help text description for the FolderBrowserDialog.
            folderBrowserDialog1.Description = "选择目标的目录";

            // Do not allow the user to create new files via the FolderBrowserDialog.
            folderBrowserDialog1.ShowNewFolderButton = false;

            // Default to the My Documents folder.
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;

            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                var folderName = folderBrowserDialog1.SelectedPath;
                cb(folderName);
            }
        }


        /// <summary>
        /// 打开jSON文件
        /// </summary>
        /// <param name="cb">成功回调</param>
        public void OpenJsonFile(Action<string> cb)
        {
            var dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.Filter = "json file (*.json)|*.*";
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Get the path of specified file
                var  filePath = dlg.FileName;
                cb(filePath);
            }
        }



        /// <summary>
        /// 保存SQL文件
        /// </summary>
        /// <param name="cb">成功回调</param>
        public void SaveSqlFile(Action<string> cb)
        {
            var dlg = new SaveFileDialog();
            dlg.Filter = "sql file (*.sql)|*.*";
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Get the path of specified file
                var filePath = dlg.FileName;
                cb(filePath);
            }
        }
    }
}
